package com.hu.biz.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CascadeIdGeneratorUtil
 * @Description cascadeId生成器
 * @author 355760
 */
public class CascadeIdGeneratorUtil {

    public static String getNewCascadeNum(List<String> cascadeIdList, String parentCascadeId) {
        int max = 0;
        if (cascadeIdList.size() > 0) {
            for (String cascadeId : cascadeIdList) {
                List<String> strings = Arrays.asList(cascadeId.split("\\."));
                String s = strings.get(strings.size() - 1);
                int integer = Integer.parseInt(s);
                if (max < integer) {
                    max = integer;
                }
            }
            max += 1;
        }

        return parentCascadeId + max + ".";
    }

    /**
     * 获取新的子级cascadeId
     *
     * @param previousCascadeId 前cascadeId
     * @param parentId          父级cascadeId
     * @return String
     */
    public static String getNewSubCascadeNum(String previousCascadeId, String parentId) {
            List<String> strings = Arrays.asList(previousCascadeId.split("\\."));
            String s = strings.get(strings.size() - 1);
            return parentId + s + ".";

    }

    private Map<String, Integer> levelMap = new HashMap<>();

    public  String generateLevelCode(String parentLevelCode) {
        int level = levelMap.getOrDefault(parentLevelCode, 0) + 1;
        levelMap.put(parentLevelCode, level);
        return parentLevelCode == null ? Integer.toString(level) : parentLevelCode + "." + level;
    }
}
